package com.my.auth.security;

/**
 * @author zhaodongchao
 * @date 2020/3/28 18:10
 **/
public class LoginContextHolder {
    private static final ThreadLocal<SecurityContext> contextHolder = new ThreadLocal<>();
    /**
     * Explicitly clears the context value from the current thread.
     */
    public static void clearContext() {
        contextHolder.remove();
    }

    /**
     * Obtain the current <code>SecurityContext</code>.
     *
     * @return the security context (never <code>null</code>)
     */
    public static SecurityContext getContext() {
        return contextHolder.get();
    }
    /**
     * Associates a new <code>SecurityContext</code> with the current thread of execution.
     *
     * @param context the new <code>SecurityContext</code> (may not be <code>null</code>)
     */
    public static void setContext(SecurityContext context) {
        contextHolder.set(context);
    }

    /**
     * Delegates the creation of a new, empty context to the configured strategy.
     */
    public static SecurityContext createEmptyContext() {
        contextHolder.set(new SecurityContextImpl());
        return contextHolder.get();
    }

}
